<template>
	<el-dialog :title="form.id ? '编辑' : '新增'" :visible.sync="visible" :close-on-click-modal="false">
		<el-form ref="form" :model="form" :rules="rules">
			<el-form-item label="配置名称" :label-width="formLabelWidth" prop="name">
				<el-input v-model="form.name" autocomplete="off" placeholder="请输入配置名称"></el-input>
			</el-form-item>
			<el-form-item label="标签名称" :label-width="formLabelWidth" prop="tagName">
				<el-input v-model="form.tagName" autocomplete="off" placeholder="请输入标签名称"></el-input>
			</el-form-item>
			<el-form-item label="选值类型" :label-width="formLabelWidth" prop="valueType">
				<fortress-radio-group v-model="form.valueType" :dictList="ConfigValueType_cmsList"></fortress-radio-group>
			</el-form-item>
			<el-form-item label="选值范围" :label-width="formLabelWidth" prop="tagValues">
				<el-input v-model="form.tagValues" autocomplete="off" placeholder="请输入选值范围"></el-input>
			</el-form-item>
			<!-- <el-form-item label="修改人" :label-width="formLabelWidth" prop="updateUser">
                <el-input
                        v-model="form.updateUser"
                        autocomplete="off"
                        placeholder="请输入修改人"
                ></el-input>
            </el-form-item>
            <el-form-item label="修改时间" :label-width="formLabelWidth" prop="updateTime">
                <el-input
                        v-model="form.updateTime"
                        autocomplete="off"
                        placeholder="请输入修改时间"
                ></el-input>
            </el-form-item> -->
			<el-form-item label="标签分组" :label-width="formLabelWidth" prop="tagGroup">
				<fortress-radio-group v-model="form.tagGroup" :dictList="TagGroup_cmsList"></fortress-radio-group>
			</el-form-item>
			<el-form-item label="排序" :label-width="formLabelWidth" prop="sort">
				<el-input v-model="form.sort" autocomplete="off" placeholder="请输入排序"></el-input>
			</el-form-item>
		</el-form>
		<div slot="footer" class="dialog-footer">
			<el-button @click="visible = false" size="medium" type="danger">取 消</el-button>
			<el-button type="primary" size="medium" @click="submitForm('form')">确 定</el-button>
		</div>
	</el-dialog>
</template>
<script>
import { cmsconfigInfo, cmsconfigSave, cmsconfigEdit } from "@/api/cms/cmsconfig.js"
import {dictList, enumList} from "@/api/public.api.js"
import * as ftVal from "@/common/fortressValidator.js"
export default {
	data() {
		return {
			visible: false,
			form: {
				id: "",
				name: "",
				tagName: "",
				valueType: "",
				tagValues: "",
				updateUser: "",
				updateTime: "",
				tagGroup: "",
				sort: "",
			},
			formLabelWidth: "120px",
			rules: {
				sort: [{ validator: ftVal.validatePInteger, trigger: "blur" }],
			},
			ConfigValueType_cmsList: [],
			TagGroup_cmsList: [],
		}
	},
	created() {},
	methods: {
		queryEnumList() {
			this.ConfigValueType_cmsList = []
			enumList("ConfigValueType", "cms").then(resp => {
				if (resp.code == 0) {
					this.ConfigValueType_cmsList = resp.data
				}
			})
			this.TagGroup_cmsList = []
      dictList("config_tag_group").then(resp => {
				if (resp.code == 0) {
					this.TagGroup_cmsList = resp.data
				}
			})
		},
		init(id) {
			this.resetForm()
			this.queryEnumList()
			//修改
			if (id) {
				cmsconfigInfo(id).then(resp => {
					if (resp.code == 0) {
						this.$nextTick(() => {
							this.form = {
								...this.form,
								...resp.data,
							}
						})
					} else {
						this.$msg({
							message: resp.msg,
							type: "error",
						})
					}
				})
			}
			this.visible = true
		},
		//提交表单数据
		submitForm() {
			this.$refs["form"].validate(valid => {
				if (valid) {
					//修改
					if (this.form.id) {
						cmsconfigEdit(this.form).then(resp => {
							if (resp.code == 0) {
								this.$message({
									type: "success",
									message: "操作成功!",
								})
								this.$emit("refreshData")
								this.visible = false
							} else {
								this.$msg({
									message: resp.msg,
									type: "error",
								})
							}
						})
						//新增
					} else {
						cmsconfigSave(this.form).then(resp => {
							if (resp.code == 0) {
								this.$message({
									type: "success",
									message: "操作成功!",
								})
								this.$emit("refreshData")
								this.visible = false
							} else {
								this.$msg({
									message: resp.msg,
									type: "error",
								})
							}
						})
					}
				} else {
					console.log("error submit!!")
					return false
				}
			})
		},
		resetForm() {
			this.form = {
				id: "",
				name: "",
				tagName: "",
				valueType: "",
				tagValues: "",
				updateUser: "",
				updateTime: "",
				tagGroup: "",
				sort: "",
			}
			this.$nextTick(() => {
				if (this.$refs["form"]) {
					this.$refs["form"].resetFields()
				}
			})
		},
	},
}
</script>
<style scoped>
.avatar-uploader .el-upload {
	border: 1px dashed #d9d9d9;
	border-radius: 6px;
	cursor: pointer;
	position: relative;
	overflow: hidden;
}
.avatar-uploader .el-upload:hover {
	border-color: #409eff;
}
.avatar-uploader-icon {
	font-size: 28px;
	color: #8c939d;
	width: 178px;
	height: 178px;
	line-height: 178px;
	text-align: center;
}
.avatar {
	width: 178px;
	height: 178px;
	display: block;
}
.el-scrollbar .el-scrollbar__view .el-select-dropdown__item {
	height: auto;
	max-height: 274px;
	padding: 7px;
	overflow: hidden;
	overflow-y: auto;
	font-weight: normal;
}
</style>
